package com.xzhuang.consumer;

import java.text.SimpleDateFormat;

/***
 * 消息队列监听--------------
 * 提供两种场景的消息监听 1 生产者，消费者模式  2，3  发布/订阅模式
 * @author zyf
 */
public class QueueListener {
	
	static SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS");
	//模拟 生产者消费者，正常消息的监听，消费
	
//public static void listerQueues() {
//		
//		try {
//			Connection connection = RabbitConfig.getConnection(true);
//			final Channel channel = connection.createChannel();
//	        channel.exchangeDeclare(ExchangeName.test_exchangeName.name(), "fanout");
//	        //声明一个随机消息队列
//	        String queueName = QueueName.test_queue111111111111.name();
//	        //绑定消息队列和消息路由
//	        channel.queueBind(queueName, ExchangeName.test_exchangeName.name(), "");
//	        Consumer consumer = new DefaultConsumer(channel) {
//	            @Override
//	            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
//	                throws IOException {
//	              String message = new String(body, "UTF-8");
//	              System.out.println(" 正常消费者 --- '" + message + "'" + sdf.format(new Date()));
//	              channel.basicAck(envelope.getDeliveryTag(), true); 
//	            }
//	          };
//	          channel.basicConsume(queueName, false, consumer);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		
//	}
	

public static void listerQueues4() {
	
//	try {
//		Connection connection = RabbitConfig.getConnection(true);
//		final Channel channel = connection.createChannel();
//        channel.exchangeDeclare("rabbitmq-exchange-order", "topic");
//        //声明一个随机消息队列
//        String queueName = "";
//        channel.queueBind(queueName, "rabbitmq-exchange-order", "order.insert");
//        Consumer consumer = new DefaultConsumer(channel) {
//            @Override
//            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)
//                throws IOException {
//              String message = new String(body, "UTF-8");
//              System.out.println(" 正常消费者 --- '" + message + "'" + sdf.format(new Date()));
//              channel.basicAck(envelope.getDeliveryTag(), true); 
//            }
//          };
//          channel.basicConsume(queueName, false, consumer);
//	} catch (Exception e) {
//		e.printStackTrace();
//	}
	
}


//	
//	public static void listerQueues2() {
//		try {
//			Connection connection = RabbitConfig.getConnection(true);
//			final Channel channel = connection.createChannel();
//	        channel.exchangeDeclare(ExchangeName.test_exchangeName.name(), "fanout");
//	        //声明一个随机消息队列
//	        String queueName = channel.queueDeclare().getQueue();
//	        //绑定消息队列和消息路由
//	        channel.queueBind(queueName, ExchangeName.test_exchangeName.name(), "");
//	        Consumer consumer = new DefaultConsumer(channel) {
//	            @Override
//	            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body)throws IOException {
//	              String message = new String(body, "UTF-8");
//	              System.out.println(" 订阅者11--- '" + message + "'" + sdf.format(new Date()));
//	              channel.basicAck(envelope.getDeliveryTag(), true); 
//	            }
//	          };
//	          channel.basicConsume(queueName, false, consumer);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//	}
//	
//	public static void listerQueues3() {
//		
//		try {
//			Connection connection = RabbitConfig.getConnection(true);
//			final Channel channel = connection.createChannel();
//	        channel.exchangeDeclare(ExchangeName.test_exchangeName.name(), "fanout");
//	        //声明一个随机消息队列
//	        String queueName = channel.queueDeclare().getQueue();
//	        //绑定消息队列和消息路由
//	        channel.queueBind(queueName, ExchangeName.test_exchangeName.name(), "");
//	        Consumer consumer = new DefaultConsumer(channel) {
//	            @Override
//	            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//	              String message = new String(body, "UTF-8");
//	              System.out.println(" 订阅者2--- '" + message + "'" + sdf.format(new Date()));
//	              //channel.basicAck(envelope.getDeliveryTag(), true); 
//	            }
//	          };
//	          channel.basicConsume(queueName, false, consumer);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		
//	}
//	
//	
}
